package mlz.e_objectstream;

import org.junit.Test;

import java.io.*;

/*
    自定义类型对象的写入和读取
 */
public class ObjectStreamDemo02 {
    @Test
    public void test01() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.obj"));
        //写入student对象
        Student stu = new Student();
        stu.setId(5);
        stu.setName("张三");
        stu.setAge(18);

        //将对象写入到流中
        oos.writeObject(stu);

        //关闭流对象
        oos.close();
    }

    @Test
    public void test02() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.obj"));
        Student stu = (Student) ois.readObject();
        System.out.println(stu);
    }
}











